package com.apes.autoparts.supplier.platform;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/***
 * 供应商平台
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierPlatform.CONST_TABLE_NAME)
public class SupplierPlatform extends Model {

    public static final String CONST_TABLE_NAME = "supplier_platform";

    @Id
    @Comment("流水码")
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商平台")
    private String id;

    @Name
    @Comment("平台代码")
    @Column(name = "code")
    private String code;

    @Name
    @Comment("平台名称")
    @Column(name = "name")
    private String name;

    @Comment("接入码")
    @Column(name = "APP_KEY")
    private String appKey;

    @Comment("密钥")
    @Column(name = "APP_SECRET")
    private String appSecret;

    @Comment("接口地址")
    @Column(name = "url")
    private String url;

    @OneToMany(mappedBy = "supplierPlatform", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<SupplierPlatformItem> supplierPlatformItems = new ArrayList<>();


    public List<SupplierPlatformItem> getSupplierPlatformItems() {
        for (SupplierPlatformItem item : supplierPlatformItems) {
            // 设置主表
            item.setSupplierPlatform(this);
        }
        return supplierPlatformItems;
    }
}
